package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var port int

// 子命令：server
var serverCmd = &cobra.Command{
	Use:   "server",
	Short: "启动一个简单的服务",
	Run: func(cmd *cobra.Command, args []string) {
		// 读取配置参数
		cfgPort := viper.GetInt("server.port")
		if cfgPort == 0 {
			cfgPort = port
		}
		fmt.Printf("Server started on port %d\n", cfgPort)
	},
}

func init() {
	// flag 参数
	serverCmd.Flags().IntVar(&port, "port", 8080, "指定服务端口")
	viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port"))

	// 将子命令注册到 root
	rootCmd.AddCommand(serverCmd)
}
